﻿(function () {
    var _inited = false;
    var _handler = "Handlers/ModuleSubFieldInfoHandler.ashx";
    var _subhandler = "Handlers/SubModuleInfoHandler.ashx";
    var _submoduleinfo = null;

    function _init() {
        _initUI();
    }

    function _handlerMsg(msg) {
        switch (msg) {
            case 0:
                //alert('操作成功');
                _getData(_submoduleinfo);
                break;
            case 100:
                alert('该字段已经存在');
                break;
            default:
                alert('操作失败');
                break;
        }
    }

    function _initUI() {
        $('#tbfieldinfo tbody').html('');
        $('#tbfieldinfo').data('data', null);
        $('#tbfieldinfo tbody tr').die().live('click', function () {
            $('#tbfieldinfo tbody tr').removeClass('active');
            $(this).addClass('active');
            var info = $(this).data('data');
            $('#tbfieldinfo').data('data', info);
        });
        $('#tbfieldinfo thead').die().live('click', function () {
            $('#tbfieldinfo tbody tr').removeClass('active');
            $('#tbfieldinfo').data('data', null);
        });

        $('#btnSubFieldAdd', '#SubFieldDialog').die().live('click', function () {
            var cb = function (params) {
                //指定子模块ID
                if (_submoduleinfo && _submoduleinfo != null) {
                    if (params && params != null) {
                        params.ModuleSubID = _submoduleinfo.AutoID;
                    }
                    return _addData(params);
                }
            };
            top.IndexScript.NewSubFieldDialog.Open(null, cb);
        });
        $('#btnSubFieldDelete', '#SubFieldDialog').die().live('click', function () {
            var mi = $('#tbfieldinfo').data('data');
            if (mi && mi != null) {
                if (confirm('确定删除该数据吗？')) {
                    _delData(mi);
                }
            }
            else {
                alert('请选择一项字段名称');
            }
        });
        $('#btnSubFieldModify', '#SubFieldDialog').die().live('click', function () {
            var mi = $('#tbfieldinfo').data('data');
            var flags = $('#btnSubFieldModify', '#SubFieldDialog').data('info');
            if (mi && mi != null) {
                var cb = function (params) {
                    return _saveData(params);
                };
                top.IndexScript.NewSubFieldDialog.Open(mi, cb, flags);
            }
            else {
                alert('请选择一项字段名称');
            }
        });
        $('#btnSubFieldTop', '#SubFieldDialog').die().live('click', function () {
            var mi = $('#tbfieldinfo').data('data');
            if (mi && mi != null) {
                if (mi.DisplayID == 1) {
                    alert('该字段已是第一字段');
                }
                else {
                    _orderData(1, mi);
                }
            }
            else {
                alert('请选择一项字段名称');
            }
        });
        $('#btnSubFieldUp', '#SubFieldDialog').die().live('click', function () {
            var mi = $('#tbfieldinfo').data('data');
            if (mi && mi != null) {
                if (mi.DisplayID == 1) {
                    alert('该字段已是第一字段');
                }
                else {
                    _orderData(2, mi);
                }
            }
            else {
                alert('请选择一项字段名称');
            }
        });
        $('#btnSubFieldDown', '#SubFieldDialog').die().live('click', function () {
            var mi = $('#tbfieldinfo').data('data');
            if (mi && mi != null) {
                if (mi.DisplayID == $('#tbfieldinfo tbody tr').length) {
                    alert('该字段已是最末字段');
                }
                else {
                    _orderData(3, mi);
                }
            }
            else {
                alert('请选择一项字段名称');
            }
        });
        $('#btnSubFieldBottom', '#SubFieldDialog').die().live('click', function () {
            var mi = $('#tbfieldinfo').data('data');
            if (mi && mi != null) {
                if (mi.DisplayID == $('#tbfieldinfo tbody tr').length) {
                    alert('该字段已是最末字段');
                }
                else {
                    _orderData(4, mi);
                }
            }
            else {
                alert('请选择一项字段名称');
            }
        });
        $('#btnSubModuleCreate', '#SubFieldDialog').die().live('click', function () {
            if ($('#tbfieldinfo tbody').html() == '') {
                alert('请添加至少一项字段名称');
            }
            else {
                if (confirm('初始化后将无法再修改，确定吗？')) {
                    _createSubModuleInfo(_submoduleinfo);
                }
            }
        });
    }

    function _createSubModuleInfo(params) {
        if (params && params != null) {
            $.ajax({
                type: "post",
                url: _subhandler,
                data: "action=CreateSubModuleInfo&Params=" + encodeURIComponent($.toJSON(params)),
                dataType: 'json',
                success: function (data, textStatus) {
                    ProcessAjaxResult(data, textStatus,
                        function (data, textStatus) {
                            switch (data) {
                                case 0:
                                    alert('操作成功');
                                    top.IndexScript.SubFieldDialog.Close();
                                    break;
                                case 100:
                                    alert('已经存在数据');
                                    break;
                                case 200:
                                    alert('没有字段');
                                    break;
                                default:
                                    alert('操作失败');
                                    break;
                            }
                        },
                        function (err, textStatus) {
                            alert('操作失败');
                        },
                        this);
                }
            });
        }
    }

    function _getData(params) {
        //Refresh current sub module id
        _submoduleinfo = params;
        if (params && params != null) {
            $.ajax({
                type: "get",
                url: _handler + "?action=GetModuleSubFieldInfo&Params=" + encodeURIComponent($.toJSON(params.AutoID)) + "&ts=" + new Date().valueOf(),
                dataType: 'json',
                async: false,
                success: function (data, textStatus) {
                    ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _bindData(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
                }
            });
        }
        else {
            _bindData(null);
        }
    }

    function _bindData(data) {
        $('#tbfieldinfo tbody').html('');
        $('#tbfieldinfo').data('data', null);
        if (data && data != null) {
            for (var i = 0; i < data.length; i++) {
                var d = data[i];
                var tr = '<tr>';
                tr += '<td>';
                tr += d.DisplayID;
                tr += '</td>';
                tr += '<td>';
                tr += d.FieldName;
                tr += '</td>';
                tr += '<td>';
                if (d.FieldType == 'I') {
                    tr += '数字';
                }
                else if (d.FieldType == 'D') {
                    tr += '日期';
                }
                else if (d.FieldType == 'B') {
                    tr += '布尔';
                }
                else {
                    tr += '字符';
                }
                tr += '</td>';
                tr += '<td>';
                tr += d.FieldLen;
                tr += '</td>';
                tr += '<td>';
                if (d.QueryFlag == 1) {
                    tr += '√';
                    //tr += '<input type="checkbox" checked="checked" disabled="disabled" />';
                }
                else {
                    tr += '×';
                    //tr += '<input type="checkbox" disabled="disabled" />';
                }
                tr += '</td>';
                tr += '<td>';
                if (d.ResultFlag == 1) {
                    tr += '√';
                    //tr += '<input type="checkbox" checked="checked" disabled="disabled" />';
                }
                else {
                    tr += '×';
                    //tr += '<input type="checkbox" disabled="disabled" />';
                }
                tr += '</td>';
                tr += '<td>';
                if (d.SpecialFlag == 1) {
                    tr += '文档路径';
                }
                else if (d.SpecialFlag == 2) {
                    tr += '厂商信息';
                }
                else if (d.SpecialFlag == 3) {
                    tr += '所属专业';
                }
                else if (d.SpecialFlag == 4) {
                    tr += '文件类型';
                }
                tr += '</td>';
                tr += '<td>';
                tr += d.IniValue;
                tr += '</td>';
                tr += '<td>';
                if (d.BlankFlag == 2) {
                    tr += '√';
                    //tr += '<input type="checkbox" checked="checked" disabled="disabled"/>';
                }
                else {
                    tr += '×';
                    //tr += '<input type="checkbox" disabled="disabled"/>';
                }
                tr += '</td>';
                tr += '</tr>';
                $('#tbfieldinfo tbody').append($(tr).data('data', d));
            }
        }
    }

    function _addData(params) {
        var result = true;
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=AddModuleSubFieldInfo&Params=" + encodeURIComponent($.toJSON(params)),
            async: false,
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                        if (data != 0) {
                            result = false;
                        }
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
        return result;
    }

    function _delData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=DelModuleSubFieldInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _saveData(params) {
        var result = true;
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=SaveModuleSubFieldInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                        if (data != 0) {
                            result = false;
                        }
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
        return result;
    }

    function _orderData(params, cinfo) {
        var trinfo = $('#tbfieldinfo tbody tr');
        if (trinfo != null && trinfo.length > 1) {
            var sfinfo = {};
            sfinfo.OrderType = params;
            sfinfo.IndexInfo = cinfo;
            sfinfo.ModuleSubFieldInfo = [];
            for (var i = 0; i < trinfo.length; i++) {
                var dr = $(trinfo[i]).data('data');
                sfinfo.ModuleSubFieldInfo.push(dr);
            }
            $.ajax({
                type: "post",
                url: _handler,
                data: "action=OrderModuleSubFieldInfo&Params=" + encodeURIComponent($.toJSON(sfinfo)),
                dataType: 'json',
                success: function (data, textStatus) {
                    ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
                }
            });
        }
    }

    this.SubFieldMgr = {
        init:
        function () {
            if (!!_inited) return;
            _init();
            _inited = true;
        },
        GetData: function (params) {
            _getData(params);
        }
    }
})();
$(function () {
    SubFieldMgr.init();
});